package com.foreveross.chameleon.task;

import org.apache.http.Header;

import android.content.Context;

import com.foreveross.chameleon.config.ChameleonConfig;
import com.foreveross.chameleon.config.ChameleonConstants;
import com.foreveross.chameleon.listener.AsyncTaskListener;
import com.foreveross.chameleon.utils.PropertiesUtil;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

/**
 * @descriptiion app下载计数
 * @author zhouzhineng
 *
 */
public class AppDownloadCountUpdateTask {

	protected Context mContext;
	protected AsyncTaskListener mAppAuthenListener;
	
	public AppDownloadCountUpdateTask(Context context,AsyncTaskListener appAuthenListener) {
		mContext = context;
		mAppAuthenListener = appAuthenListener;
	}
	
	
	public void exec(int build) {
		AsyncHttpClient httpClient = new AsyncHttpClient();
		RequestParams params = new RequestParams();
		params.add("app_id", ChameleonConfig.APP_ID);
		params.add("build", Integer.toString(build));
		httpClient.addHeader("User-Agent", "Chameleon");
		httpClient.post(ChameleonConfig.APP_DOWNLOAD_COUNT_UPDATE, params,
				new AsyncHttpResponseHandler() {

					@Override
					public void onFailure(int code, Header[] header, byte[] msg,
							Throwable thrw) {
						// TODO Auto-generated method stub
						if(mAppAuthenListener != null) {
							mAppAuthenListener.onFail(code);
						}
						
					}

					@Override
					public void onSuccess(int code, Header[] header, byte[] msg) {
						// TODO Auto-generated method stub
						if(mAppAuthenListener != null) {
							String result = new String(msg);
							mAppAuthenListener.onSucc(result);
						}
						
					}
			
		});
	}
}
